﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace EC.Merchant.Controllers
{
    using EC.Application.Interface;
    using EC.Entity;
    using EC.Entity.Request.User;

    public class HomeController : BaseController
    {
        #region 注入
        /// <summary>
        /// 用户
        /// </summary>
        private readonly LoginService _loginService;

        /// <summary>
        /// 商户
        /// </summary>
        private readonly BusinessService _businessService;

        /// <summary>
        /// 
        /// </summary>
        public HomeController(LoginService loginService, BusinessService businessService) : base(loginService)
        {
            _loginService = loginService;
            _businessService = businessService;
        }
        #endregion

        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            var user = _loginService.Get(UserId);
            if (user == null)
            {
                var response = new JResult()
                {
                    Success = true,
                    Message = "用户不存在."
                };

                return View("Error", response);
            }

            ViewBag.Account = user.Account;

            var merchant = _loginService.GetMerchant(MerchantId);
            if (merchant == null)
            {
                var response = new JResult()
                {
                    Success = true,
                    Message = "商户不存在."
                };

                return View("Error", response);
            }

            ViewBag.MerchantName = merchant.Name;
            return View();
        }

        /// <summary>
        /// 用户[重置]
        /// </summary>
        /// <returns></returns>
        public IActionResult ResetPassWord()
        {
            var viewName = "~/Views/Home/ResetPassWord.cshtml";

            var model = _loginService.Get(UserId);

            return View(viewName, model);
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult ResetPassWord(ResetPassWordRequest request)
        {
            request.Id = UserId;

            var response = _businessService.ResetPassWord(request);

            return Json(response);
        }
    }
}
